package com.abiao.shop.dao;

import com.abiao.shop.pojo.DiscountGoods;
import com.abiao.shop.pojo.Goods;
import com.abiao.shop.vojo.GoodsVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface GoodsDao {

    //获取所有商品（普通商品和秒杀商品）
    @Select("select g.*, dg.discount_price, dg.stock_count, dg.start_date, dg.end_date from discount_goods dg left join goods g on dg.goods_id = g.id")
    List<GoodsVo> goodsVoList();

    //获取所有商品
    @Select("select * from goods")
    List<Goods> getAllGoods();

    //根据id查找指定普通商品
    @Select("select * from goods where id = #{id}")
    Goods getGoodsById(long id);

    //根据id获取商品信息（普通商品和秒杀商品）
    @Select("select g.*, dg.discount_price, dg.stock_count, dg.start_date, dg.end_date from discount_goods dg left join goods g on dg.goods_id = g.id where g.id = #{goodsId}")
    GoodsVo getGoodsVoById(long goodsId);

    //商品库存减一
    @Update("update discount_goods set stock_count = stock_count-1 where goods_id = #{goodsId} and stock_count > 0")
    int reduceStock(DiscountGoods goods);
}
